import {Injectable} from '@angular/core';
import {SysConfigSearchForm} from '../entity/sys-config-search-form';
import {SysConfig} from '../entity/sys-config';
import {SysConfigTreeNode} from '../entity/sys-config-tree-node';
import {BaseTreeService} from '@sb/base/core/service/base-tree.service';
import {baseHandleError, ReturnForm} from '@sb/base';
import {Observable, of} from 'rxjs';
import {catchError} from 'rxjs/operators';
import {SFSchema} from '@delon/form';
import {SysRegion} from '../../sys-region/entity/sys-region';
import {BaseAttachmentService} from '../../sys-attachment/service/base-attachment.service';
import {SysAttachment} from '../../sys-attachment/entity/sys-attachment';

/**
 * 功能服务
 */
@Injectable({
    providedIn: 'root'
})
export class SysConfigService extends BaseTreeService<SysConfig, SysConfigSearchForm, SysConfigTreeNode> implements BaseAttachmentService {

    basePath = 'sys/config';

    generateSchema(pageId: string): Observable<ReturnForm<{ [key: string]: SFSchema; }>> {
        return this._http.post<ReturnForm<{ [key: string]: SFSchema; }>>(`${this.url}/generateSchema`, {pageId: pageId}).pipe(
            catchError(baseHandleError<ReturnForm<{ [key: string]: SFSchema; }>>('generateSchema'))
        );
    }

    uploadSchema(pageId: string, schemaText: string): Observable<ReturnForm<string>> {
        return this._http.post<ReturnForm<string>>(`${this.url}/generateSchema`, {
            pageId: pageId,
            schemaText: schemaText
        }).pipe(
            catchError(baseHandleError<ReturnForm<string>>('generateSchema'))
        );
    }

    getSchema(pageCode: string): Observable<ReturnForm<string>> {
        return this._http.post<ReturnForm<string>>(`${this.url}/getSchema`, {page: pageCode}).pipe(
            catchError(baseHandleError<ReturnForm<string>>('getSchema'))
        );
    }

    getValue(pageCode: string): Observable<ReturnForm<{ [key: string]: any; }>> {
        return this._http.post<ReturnForm<{ [key: string]: any; }>>(`${this.url}/getValue`, {page: pageCode}).pipe(
            catchError(baseHandleError<ReturnForm<{ [key: string]: any; }>>('getValue'))
        );
    }

    updateValue(pageCode: string, value: any): Observable<ReturnForm<string>> {
        return this._http.post<ReturnForm<string>>(`${this.url}/updateValue?page=${pageCode}`, JSON.stringify(value), {
            headers: {
                'Content-Type': 'application/json;charset=UTF-8'
            }
        }).pipe(
            catchError(baseHandleError<ReturnForm<string>>('updateValue'))
        );
    }

    /**
     * 根据多个附件id查询
     * @param ids 主键集
     * @param params 附加参数
     */
    getAttachments(ids: string[], params?: { [key: string]: any }): Observable<ReturnForm<SysAttachment[]>> {
        if (!ids
            || ids.length === 0
            || (ids.length === 1 && (ids[0] === 'null' || ids[0] === 'undefined'))) {
            return of({success: true, message: []} as ReturnForm<SysAttachment[]>);
        }
        return this._http.post<ReturnForm<SysAttachment[]>>(`${this.url}/getAttachments`, {
            ['attachmentIds']: ids,
            page: params['page']
        }).pipe(
            catchError(baseHandleError<ReturnForm<SysAttachment[]>>('getAttachments'))
        );
    }

    uploadAttachment(file: FormData, params?: { [key: string]: any }): Observable<ReturnForm<SysAttachment>> {
        file.append('page', params['page']);
        return this._http.post<ReturnForm<SysAttachment>>(`${this.url}/uploadAttachment`, file).pipe(
            catchError(baseHandleError<ReturnForm<SysAttachment[]>>('uploadAttachment'))
        );
    }

    getReadAttachmentUrl(id, params?: { [key: string]: any }): string {
        return `${this.url}/readAttachment/${id}?page=${params['page']}`;
    }

    getReadAttachmentThumbUrl(id, params?: { [key: string]: any }): string {
        return `${this.url}/readAttachment/thumb/${id}?page=${params['page']}`;
    }
}
